﻿using Common.Contract.Authentication.Record;
using Common.Contract.SystemConfig.Record;
using SalePerformance.Core.Data;
using SalePerformance.Model.Interface;
using SalePerformance.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;

namespace SalePerformance.Provider
{
    public class SalePerformanceProvider : GenericProvider, ISalePerformanceProvider
    {
        public List<TblSpDepositEop> RetrieveDepositByDao(string dao)
        {
            var data = repository.DbContext as SalePerformanceEntities;
            var query = from x in data.DepositEOP
                        where x.Dao.ToUpper() == dao.ToUpper()
                        select x;
            return query.ToList();
        }
        public List<TblSpLoanEop> RetrieveLoanProductByDao(string dao)
        {
            var data = repository.DbContext as SalePerformanceEntities;
            var query = from x in data.LoanEOP
                        where x.Dao.ToUpper() == dao.ToUpper() && x.GroupProduct == 1
                        select x;
            return query.ToList();
        }
        public List<TblSpLoanEop> RetrieveLoanProductProgramByDao(string dao)
        {
            var data = repository.DbContext as SalePerformanceEntities;
            var query = from x in data.LoanEOP
                        where x.Dao.ToUpper() == dao.ToUpper() && x.GroupProduct == 3
                        select x;
            return query.ToList();
        }
        public List<TblSpLoanEop> RetrieveLoanPromoProgramByDao(string dao)
        {
            var data = repository.DbContext as SalePerformanceEntities;
            var query = from x in data.LoanEOP
                        where x.Dao.ToUpper() == dao.ToUpper() && x.GroupProduct == 2
                        select x;
            return query.ToList();
        }
        public List<TblSpCashManagementEop> RetrieveCashManagementByDao(string dao)
        {
            var data = repository.DbContext as SalePerformanceEntities;
            var query = from x in data.CashManagementEops
                        where x.Dao.ToUpper() == dao.ToUpper()
                        select x;
            return query.ToList();
        }
        public List<TblSpTradeEop> RetrieveTradeByDao(string dao)
        {
            var data = repository.DbContext as SalePerformanceEntities;
            var query = from x in data.TradeEops
                        where x.Dao.ToUpper() == dao.ToUpper()
                        select x;
            return query.ToList();
        }
        public List<TblSalePerformanceKpi> RetrieveSaleKpiByDao(string dao)
        {
            var data = repository.DbContext as SalePerformanceEntities;
            var query = from x in data.SaleKPIs
                        where x.Dao.ToUpper() == dao.ToUpper() && x.Dao != "khong co DAO"
                        orderby new { x.KpiGroup, x.Order }
                        select x;
            return query.ToList();
        }
        public List<TblSalePerformanceKpi> RetrieveSaleKpiByRegion(UserRecord dao)
        {
            var data = repository.DbContext as SalePerformanceEntities;
            var query = from x in data.SaleKPIs
                        where x.Region.ToUpper().Equals(dao.Zone.ToUpper())
                        orderby new { x.KpiGroup, x.Order }
                        select x;
            return query.ToList();
        }

        public List<TblKpiMsboRaw> RetrieveKpiMsboRawByDao(out int totalRecord, UserRecord dao, FilterRecord textSearch = null, int pagesize = 0, int pageindex = 0)
        {
            var data = repository.DbContext as SalePerformanceEntities;
            var query = from x in data.KpiMsboRawDailys
                        where x.BranchCode.Equals(dao.BranchCode)
                        select x;
            totalRecord = query.Count();
            query = query.OrderBy(x => x.Dao);
            if (pagesize > 0 && pageindex >= 0)
            {
                query = query.Skip(pageindex * pagesize).Take(pagesize);
            }
            return query.ToList();
        }
        public TblKpiRankBranch RetrieveKpiRankBranchByBranch(UserRecord dao, string branch)
        {
            var data = repository.DbContext as SalePerformanceEntities;
            var query = from x in data.KpiRankBranchs
                        where x.BranchNameSme.Equals(branch) && x.Region.Equals(dao.Zone)
                        select x;
            return query.FirstOrDefault();
        }
        public string RetrieveSaleContestRankingBranchRegionGroup(UserRecord dao, int saleContestId)
        {
            var data = repository.DbContext as SalePerformanceEntities;

            return (from x in data.BilawardBranchRankingDailys
                    where x.Region.Equals(dao.Zone) && x.TypeRank.Equals("BRANCH") && x.SaleContestId == saleContestId
                    select x.GroupRegion).ToList().FirstOrDefault();
        }

        public string RetrieveSaleContestRankingRegionRegionGroup(UserRecord dao, int saleContestId)
        {
            var data = repository.DbContext as SalePerformanceEntities;

            return (from x in data.BilawardBranchRankingDailys
                    where x.Region.Equals(dao.Zone) && x.TypeRank.Equals("REGION") && x.SaleContestId == saleContestId
                    select x.GroupRegion).ToList().FirstOrDefault();
        }
        public List<TblSaleContestBilawardRankingDaily> RetrieveSaleContestRankingMSBOByDao(int saleContestId)
        {
            var data = repository.DbContext as SalePerformanceEntities;
            var query = from x in data.BilawardBranchRankingDailys
                        where x.TypeRank.Equals("MSBO") && x.SaleContestId == saleContestId
                        select x;

            //switch (saleContestId)
            //{
            //    case 1:
            //        query = query.OrderBy(x => new { x.XepHang, x.Region });
            //        break;
            //    default:
            query = query.OrderBy(x => new { x.Region, x.XepHang });
            //        break;
            //}
            return query.ToList();
        }
        public List<TblSaleContestBilawardRankingDaily> RetrieveSaleContestRankingBranchByDao(int saleContestId)
        {
            var data = repository.DbContext as SalePerformanceEntities;

            var query = from x in data.BilawardBranchRankingDailys
                        where x.TypeRank.Equals("BRANCH") && x.SaleContestId == saleContestId
                        select x;
            query = query.OrderBy(x => new { x.GroupRegion, x.XepHang });
            return query.ToList();
        }
        public List<TblSaleContestBilawardRankingDaily> RetrieveSaleContestRankingRegionByDao(int saleContestId)
        {
            var data = repository.DbContext as SalePerformanceEntities;

            var query = from x in data.BilawardBranchRankingDailys
                        where x.TypeRank.Equals("REGION") && x.SaleContestId == saleContestId
                        select x;
            query = query.OrderBy(x => new { x.GroupRegion, x.XepHang });
            return query.ToList();
        }
        public List<TblKpiRawDnbqDaily> RetrieveRawDataDNBQByDao(out int totalRecord, string dao, FilterRecord textSearch = null, string lang = null, int pagesize = 0, int pageindex = 0, DateTime? fromDate = null, string type = null)
        {
            var data = repository.DbContext as SalePerformanceEntities;
            var query = from x in data.KpiRawDnbqDailys
                        where (x.Dao.Equals(dao) || x.DaoCif.Equals(dao)) && x.App.Equals("LOAN")
                        select x;
            totalRecord = query.Count();
            query = query.OrderBy(x => x.Cif);
            if (pagesize > 0 && pageindex >= 0)
            {
                query = query.Skip(pageindex * pagesize).Take(pagesize);
            }
            return query.ToList();
        }
        public List<TblKpiRawDnbqDaily> RetrieveRawDataHDBQByDao(out int totalRecord, string dao, FilterRecord textSearch = null, string lang = null, int pagesize = 0, int pageindex = 0, DateTime? fromDate = null, string type = null)
        {
            var data = repository.DbContext as SalePerformanceEntities;
            var query = from x in data.KpiRawDnbqDailys
                        where (x.Dao.Equals(dao) || x.DaoCif.Equals(dao)) && x.App.Equals("DEPOSIT")
                        select x;
            totalRecord = query.Count();
            query = query.OrderBy(x => x.Cif);
            if (pagesize > 0 && pageindex >= 0)
            {
                query = query.Skip(pageindex * pagesize).Take(pagesize);
            }
            return query.ToList();
        }
        public List<TblKpiRawDnbqDaily> RetrieveRawDataDNTCBQByDao(out int totalRecord, string dao, FilterRecord textSearch = null, string lang = null, int pagesize = 0, int pageindex = 0, DateTime? fromDate = null, string type = null)
        {
            var data = repository.DbContext as SalePerformanceEntities;
            var query = from x in data.KpiRawDnbqDailys
                        where (x.Dao.Equals(dao) || x.DaoCif.Equals(dao)) && x.App.Equals("LOANUNSECURED")
                        select x;
            totalRecord = query.Count();
            query = query.OrderBy(x => x.Cif);
            if (pagesize > 0 && pageindex >= 0)
            {
                query = query.Skip(pageindex * pagesize).Take(pagesize);
            }
            return query.ToList();
        }

        public List<TblKpiRawDnbqDaily> RetrieveRawDataBadbankByDao(out int totalRecord, string dao, FilterRecord textSearch = null, string lang = null, int pagesize = 0, int pageindex = 0, DateTime? fromDate = null, string type = null)
        {
            var data = repository.DbContext as SalePerformanceEntities;
            var query = from x in data.KpiRawDnbqDailys
                        where (x.Dao.Equals(dao) || x.DaoCif.Equals(dao)) && x.App.Equals("LOANBADBANK")
                        select x;
            totalRecord = query.Count();
            query = query.OrderBy(x => x.Cif);
            if (pagesize > 0 && pageindex >= 0)
            {
                query = query.Skip(pageindex * pagesize).Take(pagesize);
            }
            return query.ToList();
        }
        public List<TblKpiRawCardDaily> RetrieveRawDataCardByDao(out int totalRecord, string dao, FilterRecord textSearch = null, string lang = null, int pagesize = 0, int pageindex = 0, DateTime? fromDate = null, string type = null)
        {
            var data = repository.DbContext as SalePerformanceEntities;
            var query = from x in data.KpiRawCardDailys
                        where (x.Dao.Equals(dao) || x.DaoCif.Equals(dao))
                        select x;
            totalRecord = query.Count();
            query = query.OrderBy(x => x.Cif);
            if (pagesize > 0 && pageindex >= 0)
            {
                query = query.Skip(pageindex * pagesize).Take(pagesize);
            }
            return query.ToList();
        }
        public List<TblKpiRawKhhdDaily> RetrieveRawDataKHHDByDao(out int totalRecord, string dao, FilterRecord textSearch = null, string lang = null, int pagesize = 0, int pageindex = 0, DateTime? fromDate = null, string type = null)
        {
            var data = repository.DbContext as SalePerformanceEntities;
            var query = from x in data.KpiRawKhhdDailys
                        where x.Dao.Equals(dao)
                        select x;
            totalRecord = query.Count();
            query = query.OrderBy(x => x.Dao);
            if (pagesize > 0 && pageindex >= 0)
            {
                query = query.Skip(pageindex * pagesize).Take(pagesize);
            }
            return query.ToList();
        }
        public List<TblSaleContestBilAwardRaw> RetrieveRawDataSaleContestBilAwardByDao(out int totalRecord, string dao, FilterRecord textSearch = null, string lang = null, int pagesize = 0, int pageindex = 0, DateTime? fromDate = null, string type = null)
        {
            var data = repository.DbContext as SalePerformanceEntities;
            var query = from x in data.SaleContestBilAwardRaws
                        where x.Dao.Equals(dao)
                        select x;
            totalRecord = query.Count();
            query = query.OrderBy(x => new { x.CheckTime, x.Cif });
            if (pagesize > 0 && pageindex >= 0)
            {
                query = query.Skip(pageindex * pagesize).Take(pagesize);
            }
            return query.ToList();
        }
    }
}
